<template>
  <div class="app-container">

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="info"
          plain
          icon="el-icon-sort"
          size="mini"
          @click="toggleExpandAll"
        >展开/折叠</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table
      v-if="refreshTable"
      v-loading="loading"
      :data="deptList"
      row-key="deptId"
      :default-expand-all="isExpandAll"
      :tree-props="{children: 'children', hasChildren: 'hasChildren'}"
      :max-height="maxHeight"
      style="width: 100%;"
    >
      <el-table-column prop="deptName" label="部门名称" width="260"></el-table-column>
      <el-table-column prop="deptId" label="部门编号" width="260"></el-table-column>
      <el-table-column prop="accumulatedMaleCount" label="男性" width="100"></el-table-column>
      <el-table-column prop="accumulatedFemaleCount" label="女性" width="100"></el-table-column>
      <el-table-column prop="accumulatedTotalCount" label="总人数" width="100"></el-table-column>
      <el-table-column prop="maleRatio" label="男性比例" width="200"></el-table-column>
      <el-table-column prop="femaleRatio" label="女性比例" width="200"></el-table-column>
    </el-table>

  </div>
</template>

<script>
import Treeselect from "@riophae/vue-treeselect";
import "@riophae/vue-treeselect/dist/vue-treeselect.css";
import { getcountbygenderApi } from "@/api/Empment/DataStatistics";

export default {
  name: "Dept",
  dicts: ['sys_normal_disable'],
  components: { Treeselect },
  data() {
    return {
      // getDataStatisticsApi的返回数据
      deptData: [],
      // 遮罩层
      loading: false,
      // 显示搜索条件
      showSearch: true,
      // 表格树数据
      deptList: [],
      // 部门树选项
      deptOptions: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 是否展开，默认全部展开
      isExpandAll: true,
      // 重新渲染表格状态
      refreshTable: true,
      // 表格最大高度占屏幕的80%
      maxHeight: window.innerHeight * 0.8,
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询部门列表 */
    async getList() {
      this.loading = true;
      getcountbygenderApi().then(response => {
          this.deptList = this.handleTree(response.data, "deptId");
          this.loading = false;
    },);
    },
        /** 转换部门数据结构 */
        normalizer(node){
        if (node.children && !node.children.length) {
          delete node.children;
        }
        return {
          id: node.deptId,
          label: node.deptName,
          children: node.children
        };
      },
    /** 展开/折叠操作 */
      /** 展开/折叠操作 */
      toggleExpandAll() {
        this.refreshTable = false;
        this.isExpandAll = !this.isExpandAll;
        this.$nextTick(() => {
          this.refreshTable = true;
        });
      }
  }
};
</script>
